﻿// @author WangHongQi
// Created on: 2022-11-14 16:09

using UnityEngine;

public class HUDMgr
{
    private static HUDMgr _instance = null;
    
    public static HUDMgr Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new HUDMgr();
            }
            return _instance;
        }
    }
    
    private bool _isInited;
    public HUDSetting HUDSetting;
    private HUDNumberRender HUDNumberRender;
    private bool _hudVisible;
    
    private HUDMgr()
    {
        _isInited = false;
        
        HUDSetting = new HUDSetting();
        HUDNumberRender = new HUDNumberRender();
        _hudVisible = false;
    }
    
    public void Init(HUDAniSetting hudAniSetting)
    {
        if (_isInited)
        {
            return;
        }
        _isInited = true;

        HUDMesh.OnEnterGame();
        // 预调用一下，后面加载资源会卡顿。
        AddHUDNumber(EHUDRenderType.NORMAL_ATTACK, 1, new Vector3(-99999f, -99999f, -99999f), false, false, 0);
        InitSetting(hudAniSetting);
        _hudVisible = true;
    }

    private void InitSetting(HUDAniSetting hudAniSetting)
    {
        HUDSetting.InitSetting(hudAniSetting);
        HUDNumberRender.InitHUDSetting(HUDSetting);
    }

    public void ApplySetting(HUDAniSetting hudAniSetting)
    {
        InitSetting(hudAniSetting);
    }
    
    public void Deinit()
    {
        if (!_isInited)
        {
            return;
        }
        _isInited = false;
        
        HUDMesh.OnLeaveGame();
        HUDNumberRender.OnLeaveGame();
        _hudVisible = false;
    }

    public bool HUDVisible
    {
        set
        {
            if (_hudVisible != value)
            {
                HUDNumberRender.HUDNumberVisible = value;
            }
            _hudVisible = value;
        }
        get { return _hudVisible; }
    }

    public void Update(float deltaTime)
    {
        if (!_isInited)
        {
            return;
        }
        
        HUDNumberRender.UpdateLogic(deltaTime);
    }

    public void AddHUDNumber(EHUDRenderType type, int number, Vector3 pos, bool showAdd, bool showSub, float tempScale)
    {
        HUDNumberRender.AddHUDNumber(null, (int)type, number, pos, showAdd, showSub, tempScale);
    }
}